import os


class BatchRename():

    def __init__(self):
        self.path = 'D:/yolov5/VOC2007/JEPGImages'

    def rename(self):
        filelist = os.listdir(self.path)
        filelist.sort()
        total_num = len(filelist)
        i = 0
        for item in filelist:
            if item.endswith('.jpg'):  # 要识别的文件格式尾缀
                src = os.path.join(os.path.abspath(self.path), item)
                s = str(i)
                s = s.zfill(6)
                dst = os.path.join(os.path.abspath(self.path), s + '.jpg')

                try:
                    os.rename(src, dst)
                    print('converting %s to %s ...' % (src, dst))
                    i = i + 1
                except:
                    continue
        print('total %d to rename & converted %d pngs' % (total_num, i))


if __name__ == '__main__':
    demo = BatchRename()
    demo.rename()
